# -*- coding: utf-8 -*-
import scrapy
from xicidaili.items import XicidailiItem


class YamaxunSpider(scrapy.Spider):
    name = 'yamaxun'
    allowed_domains = ['amazon.cn']
    start_urls = [
        'https://www.amazon.cn/s?k=python&i=digital-text&__mk_zh_CN=%E4%BA%9A%E9%A9%AC%E9%80%8A%E7%BD%91%E7%AB%99&qid=1595062181&ref=sr_pg_1']

    def parse(self, response):
        content = response.xpath('//*[@id="search"]/div[1]/div[2]/div/span[3]/div[2]/div/div/span/div')
        for i in content:
            item = XicidailiItem()
            item['name'] = i.xpath('./div/div[2]/div[2]/div/div[1]/div/div/div[1]/h2/a/span/text()').get()
            item['url'] = i.xpath('./div/div[2]/div[2]/div/div[1]/div/div/div[1]/h2/a/@href').get()
            yield scrapy.Request(response.urljoin(item['url']), callback=self.tiqu, meta={'item': item})

    def tiqu(self, response):
        item = response.meta['item']
        item['content'] = response.xpath('//*[@id="bylineInfo"]/span[2]/a/text()').get()
        yield item
